package com.nccsjz.back.bulletin.web;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.Bulletin;
import com.nccsjz.utils.Pager;
import com.nccsjz.back.bulletin.service.BulletinService;

@SuppressWarnings("serial")
public class BulletinAction extends BaseAction{
	//存放url中的参数
	private long id;
	//存放分页信息
    private Pager pager;
    //页码
    private int pageNo=1;
    //每页多少行
    private int pageSize=40;
    //判断标识
    private String flag;
    //存放多选中的id
    private String[] checkbox;
    private Bulletin bulletin;
    private BulletinService bulletinService;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String[] getCheckbox() {
		return checkbox;
	}
	public void setCheckbox(String[] checkbox) {
		this.checkbox = checkbox;
	}
	public Bulletin getBulletin() {
		return bulletin;
	}
	public void setBulletin(Bulletin bulletin) {
		this.bulletin = bulletin;
	}
	//分页显示所有bulletin
    public String list_Bulletin() throws Exception{
    	bulletinService=new BulletinService();
    	pager=bulletinService.getBulletin(pageSize, pageNo);
    	return SUCCESS;
    }
    //添加公告
    public String add_Bulletin()throws Exception{
    	bulletinService=new BulletinService();
    	boolean flag=bulletinService.addBulletin(bulletin);
    	if(flag){
    		return "list";
    	}
    	return ERROR;
    }
    //删除公告
    public String delete_Bulletin() throws Exception{
    	bulletinService=new BulletinService();
    	boolean flag=bulletinService.deleteBulletin(id);
    	if(flag){
    		return "list";
    	}else{
    	return ERROR;
    	}
    }
    //查看和修改视图
    public String view_Bulletin() throws Exception{
    	bulletinService=new BulletinService();
    	if(id!=0&&null!=flag){
    		bulletin=bulletinService.getBulletin(id);
    		return "edit";
    	}else{
    		bulletin=bulletinService.getBulletin(id);
    	    return "view";
    	}
    }
    //修改bulletin
    public String edit_Bulletin() throws Exception{
    	bulletinService=new BulletinService();
    	boolean flag=bulletinService.updateBulletin(bulletin);
    	if(flag){
    		return "list";
    	}else{
    	return ERROR;
    	}
    }
    //批量删除的处理
    public String deleteBatch_Bulletin() throws Exception{
    	bulletinService=new BulletinService();
    	List<Long> list=new ArrayList<Long>();
    	for(String s:checkbox){
    		 list.add(Long.parseLong(s));
    	}
    	boolean flag=bulletinService.batchDeleteBulletin(list);
    	if(flag){
    		return "list";
    	}else{
    	return ERROR;
    	}
    }
    
    /**
     * 查看更多公告
     * 
     * @return
     * @throws SQLException
     */
    public String viewMore_Bulletin() throws SQLException{
    	bulletinService=new BulletinService();
    	pager=bulletinService.getBulletin(pageSize, pageNo);
    	return "viewMore";
    }
}
